package model.player;

import java.util.LinkedList;
import java.util.List;

public class UnitTypeManager implements SubTypeManager 
{
	private List<UnitManager> unitManagers;
	
	private int umPtr;
	
	public UnitTypeManager()
	{
		unitManagers = new LinkedList<UnitManager>();
		
		umPtr = 0;
	}

	public void addUnitManager(UnitManager um)
	{
		unitManagers.add(um);
	}
	
	@Override
	public CyclableManager getCurSubType()
	{
		if (!hasUnits())
			return null;
		
		while (unitManagers.get(umPtr) == null || unitManagers.get(umPtr).isEmpty())
		{
			umPtr = (umPtr + 1) % unitManagers.size();
		}
		
		return unitManagers.get(umPtr);
	}

	@Override
	public CyclableManager getNextSubType() 
	{
		if (!hasUnits())
			return null;
		
		umPtr = (umPtr + 1) % unitManagers.size();
		
		while (unitManagers.get(umPtr) == null || unitManagers.get(umPtr).isEmpty())
		{
			umPtr = (umPtr + 1) % unitManagers.size();
		}
		
		return unitManagers.get(umPtr);
	}

	@Override
	public CyclableManager getPrevSubType() 
	{
		if (!hasUnits())
			return null;
		
		umPtr = (umPtr - 1 + unitManagers.size()) % unitManagers.size();
		
		while (unitManagers.get(umPtr) == null || unitManagers.get(umPtr).isEmpty())
		{
			umPtr = (umPtr - 1 + unitManagers.size()) % unitManagers.size();
		}
		
		return unitManagers.get(umPtr);
	}
	
	public boolean hasUnits() 
	{
		for (UnitManager um : unitManagers)
			if (!um.isEmpty())
				return true;
		
		return false;
	}

}
